
#require 'scruffy'

class GraphController < ApplicationController
  
  def burndown
    
    @sprint = Sprint.find_by_id(params[:id])
    
    business_value = 0
    complexity = 0
    
    @sprint.histories.each { |history|  
      if (  history.business_value != nil )
        business_value += history.business_value
      end
      if (  history.complexity != nil )
        complexity += history.complexity
      end
    }
    
    puts complexity
    
    #{ sum += history.business_value  }
    
    g = Gruff::Line.new
    
    #g.width  100
    
    g.title = @sprint.goal
    #g.no_data_message = :false
    labels = Hash.new
    labels[0] = nil;
    
    #puts g.theme_37signals()    
    
    tasks = Array.new
    done = Array.new
    #//tasks = Array.new
    
    #@sprints.history
    
    #labels[1] = @sprint.start_at.to_s
    
    if @sprint.start_at != nil then
      day = @sprint.start_at 
      #lastDay = Math.min(@sprint.ends_at, Date.today )
      for i in 1..10
        
        sum_done = 0
        sum_undone = 0
      
          labels[i] = day.strftime("%d/%m")
          day = day.next()


        if ( day <= Date.today )  
  

          @sprint.histories.each do |history|
            task = history.last_task(day)
            if ( task != nil ) 
              sum_done += task.done
              sum_undone += task.undone
            end 
          end
        
       
          done.push(sum_done)
          tasks.push(sum_undone)
  
        end
                
        
        
        
        
        #@sprint.start_at
        #labels.push day.strftime("%d/%m")
        #  
        #puts day
      end
    end
    g.title = @sprint.goal    
    
    #g.data("Complexity", [nil])
    
    g.data("Tasks", tasks)
    g.data("Tasks done", done)

    g.data("Business Value", [business_value, business_value, 100, 100,nil,nil,nil,nil,nil,nil,nil,])
    
    g.labels = labels
    #{0 => '01/01', 1 => '02/01', 2 => '03/01'}
    send_data(g.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "gruff.png")
    
  end
end
